<template>
  <div>
    <h1>拼团活动列表(商家)</h1>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="活动名称">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入活动名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="活动开始时间">
          <el-date-picker
            v-model="searchForm.beginTime"
            type="datetime"
            placeholder="请选择开始时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="活动结束时间">
          <el-date-picker
            v-model="searchForm.endTime"
            type="datetime"
            placeholder="请选择结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getGroupJoin()">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table :data="groupJoinData" border style="width: 100%">
        <el-table-column prop="joinId" label="Id" />
        <el-table-column prop="joinName" label="活动名称" />
        <el-table-column prop="joinTip" label="描述" />
        <el-table-column prop="unitName" label="单位" />
        <el-table-column prop="imageList" label="轮播图">
          <template #default="scope">
            <el-image
              v-for="item in scope.row.imageList"
              :key="item"
              :src="item"
              style="width: 50px; height: 50px"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="beginTime" label="开始时间" />
        <el-table-column prop="endTime" label="结束时间" />
        <el-table-column prop="groupNum" label="参团人数" />
        <el-table-column prop="buyMax" label="购买限制人数" />
        <el-table-column prop="virtualNum" label="剩余参团人数" />
        <el-table-column prop="prodPrice" label="商品价格" />
        <el-table-column prop="prodName" label="商品名称" />
        <el-table-column prop="kindName" label="运送类型" />
        <el-table-column prop="payName" label="支付配置" />
        <el-table-column prop="sendMoney" label="支付金额" />
        <el-table-column prop="joinTop" label="排序" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="onUpdateJoin(scope.row)">
              修改
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="ondeleteById(scope.row)"
            >
              删除
            </el-button>
            <el-button
              type="success"
              size="small"
              @click="onAddGroupJoin(scope.row)"
              >参团</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination
        v-model:current-page="searchForm.pageNum"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加拼团活动" width="1000">
        <el-form :model="form" :rules="rules" ref="ruleFormRef">
          <el-form-item
            label="活动名称"
            prop="joinName"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.joinName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="描述"
            prop="joinTip"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.joinTip" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="单位"
            prop="prodUnit"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.prodUnit"
              placeholder="请选择单位"
              style="width: 240px"
            >
              <el-option
                v-for="item in unitData"
                :key="item.dictType"
                :label="item.dictName"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="商品"
            prop="prodInfoId"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.prodInfoId"
              placeholder="请选择商品"
              style="width: 240px"
            >
              <el-option
                v-for="item in prodinfoData"
                :key="item.infoId"
                :label="item.prodName"
                :value="item.infoId"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="运货类型"
            prop="sendKind"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.sendKind"
              placeholder="请选择运货类型"
              style="width: 240px"
            >
              <el-option
                v-for="item in kindData"
                :key="item.dictType"
                :label="item.dictName"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="支付方式"
            prop="sendPay"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.sendPay"
              placeholder="请选择支付方式"
              style="width: 240px"
            >
              <el-option
                v-for="item in payData"
                :key="item.dictType"
                :label="item.dictName"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="参团人数"
            prop="groupNum"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.groupNum" :min="1" />
          </el-form-item>
          <el-form-item
            label="购买限制人数"
            prop="buyMax"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.buyMax" :min="1" />
          </el-form-item>
          <el-form-item
            label="剩余参团人数"
            prop="virtualNum"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.virtualNum" :min="1" />
          </el-form-item>
          <el-form-item
            label="商品价格"
            prop="prodPrice"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.prodPrice" :min="1" />
          </el-form-item>
          <el-form-item
            label="支付金额"
            prop="sendMoney"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.sendMoney" :min="1" />
          </el-form-item>
          <el-form-item
            label="排序"
            prop="joinTop"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.joinTop" :min="1" />
          </el-form-item>
          <el-form-item
            label="开始时间"
            prop="beginTime"
            :label-width="formLabelWidth"
          >
            <el-date-picker
              v-model="form.beginTime"
              type="datetime"
              placeholder="请选择开始时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item
            label="结束时间"
            prop="endTime"
            :label-width="formLabelWidth"
          >
            <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="请选择结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item
            label="轮播图"
            prop="joinImages"
            :label-width="formLabelWidth"
          >
            <el-upload
              v-model:file-list="fileList"
              action="http://localhost:10010/group/groupJoin/uploadFile"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :headers="headers"
            >
              <el-icon><Plus /></el-icon>
            </el-upload>

            <el-dialog v-model="dialogVisible">
              <img w-full :src="form.joinImages" alt="form.joinImages" />
            </el-dialog>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              添加
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        v-model="dialogUpdateFormVisible"
        title="修改拼团活动"
        width="500"
      >
        <el-form :model="form" :rules="rules" ref="ruleFormRef">
          <el-form-item
            label="活动名称"
            prop="joinName"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.joinName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="描述"
            prop="joinTip"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.joinTip" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="单位"
            prop="prodUnit"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.prodUnit"
              placeholder="请选择单位"
              style="width: 240px"
            >
              <el-option
                v-for="item in unitData"
                :key="item.dictType"
                :label="item.dictName"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="商品"
            prop="prodInfoId"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.prodInfoId"
              placeholder="请选择商品"
              style="width: 240px"
            >
              <el-option
                v-for="item in prodinfoData"
                :key="item.infoId"
                :label="item.prodName"
                :value="item.infoId"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="运货类型"
            prop="sendKind"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.sendKind"
              placeholder="请选择运货类型"
              style="width: 240px"
            >
              <el-option
                v-for="item in kindData"
                :key="item.dictType"
                :label="item.dictName"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="支付方式"
            prop="sendPay"
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.sendPay"
              placeholder="请选择支付方式"
              style="width: 240px"
            >
              <el-option
                v-for="item in payData"
                :key="item.dictType"
                :label="item.dictName"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="参团人数"
            prop="groupNum"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.groupNum" :min="1" />
          </el-form-item>
          <el-form-item
            label="购买限制人数"
            prop="buyMax"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.buyMax" :min="1" />
          </el-form-item>
          <el-form-item
            label="剩余参团人数"
            prop="virtualNum"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.virtualNum" :min="1" />
          </el-form-item>
          <el-form-item
            label="商品价格"
            prop="prodPrice"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.prodPrice" :min="1" />
          </el-form-item>
          <el-form-item
            label="支付金额"
            prop="sendMoney"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.sendMoney" :min="1" />
          </el-form-item>
          <el-form-item
            label="排序"
            prop="joinTop"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.joinTop" :min="1" />
          </el-form-item>
          <el-form-item
            label="开始时间"
            prop="beginTime"
            :label-width="formLabelWidth"
          >
            <el-date-picker
              v-model="form.beginTime"
              type="datetime"
              placeholder="请选择开始时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item
            label="结束时间"
            prop="endTime"
            :label-width="formLabelWidth"
          >
            <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="请选择结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item
            label="轮播图"
            prop="joinImages"
            :label-width="formLabelWidth"
          >
            <el-upload
              v-model:file-list="fileList"
              action="http://localhost:10010/group/groupJoin/uploadFile"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :headers="headers"
            >
              <el-icon><Plus /></el-icon>
            </el-upload>

            <el-dialog v-model="dialogVisible">
              <img w-full :src="form.joinImages" alt="form.joinImages" />
            </el-dialog>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogUpdateFormVisible = false">取消</el-button>
            <el-button type="primary" @click="onUpdateById()"> 修改 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
  getGroupJoinList,
  addGroupJoin,
  updateGroupJoin,
  deleteByjoinId,
  getDictList,
  getProdInfo,
} from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
const ruleFormRef = ref<FormInstance>();
import { Plus } from "@element-plus/icons-vue";

import type { UploadProps, UploadUserFile } from "element-plus";
const headers = ref({
  token: sessionStorage.getItem("token"),
});
//获取轮播图
const fileList = ref<UploadUserFile[]>([]);
const dialogVisible = ref(false);
const handleRemove: UploadProps["onRemove"] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
};

const handlePictureCardPreview: UploadProps["onPreview"] = (uploadFile) => {
  form.value.joinImages = uploadFile.url!;
  dialogVisible.value = true;
};
const onUpdateJoin = (row: any) => {
  Object.assign(form.value, row);
  dialogUpdateFormVisible.value = true;
};
const onUpdateById = () => {
  updateGroupJoin(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      dialogUpdateFormVisible.value = false;
      getGroupJoin();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const ondeleteById = (row: any) => {
  ElMessageBox.confirm("您确定要删除么?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteByjoinId(row.joinId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          getGroupJoin();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};

const dialogFormVisible = ref(false);
const dialogUpdateFormVisible = ref(false);
const formLabelWidth = "140px";

const form = ref({
  beginTime: "",
  buyMax: null,
  endTime: "",
  expireTime: null,
  groupNum: null,
  groupPrice: null,
  joinImages: "",
  joinName: "",
  joinTip: "",
  joinTop: null,
  prodInfoId: null,
  prodPrice: null,
  prodUnit: null,
  sendKind: null,
  sendMoney: null,
  sendPay: null,
  virtualNum: null,
});
const onAddGroupJoin = (row: any) => {
  form.value = {
    beginTime: "",
    buyMax: null,
    endTime: "",
    expireTime: null,
    groupNum: null,
    groupPrice: null,
    joinImages: "",
    joinName: "",
    joinTip: "",
    joinTop: null,
    prodInfoId: null,
    prodPrice: null,
    prodUnit: null,
    sendKind: null,
    sendMoney: null,
    sendPay: null,
    virtualNum: null,
  };
  form.value.joinImages = "";
  dialogFormVisible.value = true;
};

const rules = ref<FormRules>({
  joinName: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
  beginTime: [
    { required: true, message: "请输入开始时间名称", trigger: "blur" },
  ],
  endTime: [
    { required: true, message: "请输入活动结束时间名称", trigger: "blur" },
  ],
  buyMax: [{ required: true, message: "请输入购买数量", trigger: "blur" }],
  exprireTime: [
    { required: true, message: "请输入活动限制时间", trigger: "blur" },
  ],
  groupNum: [{ required: true, message: "请输入参团人数", trigger: "blur" }],
  groupPrice: [{ required: true, message: "请输入加团金额", trigger: "blur" }],
  // joinImages: [
  //   {
  //     required: true,
  //     validator: (rule: any, value: any, callback: any) => {
  //       if (!value) {
  //         return callback(new Error("请先选择图片"));
  //       } else {
  //         callback();
  //       }
  //     },
  //   },
  // ],
  joinTip: [{ required: true, message: "请输入活动简介", trigger: "blur" }],
  joinTop: [{ required: true, message: "请输入活动排序", trigger: "blur" }],
  prodInfoId: [{ required: true, message: "请选择商品", trigger: "change" }],
  prodPrice: [{ required: true, message: "请输入商品价格", trigger: "blur" }],
  prodUnit: [{ required: true, message: "请选择商品单位", trigger: "change" }],
  sendKind: [{ required: true, message: "请选择运货类型", trigger: "change" }],
  sendMoney: [{ required: true, message: "请输入运货金额", trigger: "blur" }],
  sendPay: [{ required: true, message: "请选择支付设置", trigger: "change" }],
  virtualNum: [
    { required: true, message: "请输入剩余参团人数", trigger: "blur" },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      const imageList = ref([]);
      fileList.value.forEach((item) => {
        imageList.value.push(item.response.data);
      });
      form.value.joinImages = JSON.stringify(imageList.value);
      if (fileList.value.length <= 0) {
        ElMessage.error("图片不能为空");
        return;
      }
      addGroupJoin(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          getGroupJoin();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const groupJoinData = ref([]);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
  beginTime: null,
  endTime: null,
});
const total = ref(0);
const getGroupJoin = () => {
  getGroupJoinList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      groupJoinData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  getGroupJoin();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  getGroupJoin();
};
const unitData = ref([]);
const prodinfoData = ref([]);
const kindData = ref([]);
const payData = ref([]);
onMounted(() => {
  getDictList(2).then((res) => {
    if (res.data.code == 200) {
      unitData.value = res.data.data;
    }
  });
  getDictList(16).then((res) => {
    if (res.data.code == 200) {
      kindData.value = res.data.data;
    }
  });
  getDictList(17).then((res) => {
    if (res.data.code == 200) {
      payData.value = res.data.data;
    }
  });
  getProdInfo().then((res) => {
    if (res.data.code == 200) {
      prodinfoData.value = res.data.data;
    }
  });
  getGroupJoin();
});
</script>

<style scoped></style>
